<template>
	<view>
		<!-- <mine-head title="每日提醒"></mine-head> -->
		<view-head Title="每日提醒" :isShowR="false" @titleBack="titleBack" titleR="添加"></view-head>
		<view class="content">
			<view class="" v-if="list.length>0">
				<u-swipe-action class="list" :show="item.show" :index="index"
					v-for="(item, index) in list" :key="item.id" 
					@click="delClick(item.id)" @open="open"
					:options="options"
				>
					<view class="item">
						<u-switch class="fr" style="margin-top: 40rpx;" v-model="item.isOpen" @change="switchChange($event,index)" size="50" active-color="#6387FF" inactive-color="#eee"></u-switch>
						<view class="text" @click="toEdit(item)">
							<view class="title">
								{{item.repeatTitle}}
							</view>
							<view class="time _df _ac">
								{{item.remindTime}}
								<span>{{item.repeatType}}</span>
							</view>
							
						</view>
					</view>
					
				</u-swipe-action>
			</view>
			<view class="" v-else style="padding-top: 200rpx;">
				<u-empty text="暂无提醒" mode="list"></u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import moment from "moment";
	export default {
		data() {
			return {
				list:[],
				options: [
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
				pushTitle:'',
				pushContent:'',
				pushPayload:'',
				dateList:[]
			};
		},
		onLoad(options) {
			
		},
		onShow() {
			this.getDays()
			
		},
		onHide() {
			let isHide = uni.getStorageSync('isAppHide')
			console.log(isHide)
			// if(isHide){
			// 	uni.connectSocket({
			// 		url: 'appUserRemind-list',
			// 		header: {
			// 			'content-type': 'application/json'
			// 		},
			// 		protocols: ['protocol1'],
			// 		method: 'GET',
			// 		success:(res)=>{
			// 			console.log(res)
			// 			this.getDays()
			// 		},
			// 		fail(err) {
			// 			console.log(err)
			// 		}
			// 	});
			// }
			

		},
		methods:{
			toEdit(e){
				let obj = JSON.stringify(e)
				uni.navigateTo({
					url:'/pages/tabbar/mine/warn-edit?data='+obj
				})
			},
			getList(){
				let that = this
				uni.$_get({
					url: "appUserRemind-list",
					that: that,
					needlogin: false,
					success(res) {
						if(res.code == 0){
							res.data.map(item =>{
								item.show = false
								if(item.status == 1){
									let arr = []
									item.remindTimeList.map((item2,index2) =>{
										item2 = that.dateList.find(aa => aa.week == item2).date + ' ' + item.remindTime
										arr.push(item2)
									})
									item.timeList = arr
									if(item.remindType == 1){
										item.pushTitle = '正念提醒'
										item.pushContent = '今日正念时间到啦，请进入无忧开始正念吧。'
										item.pushPayload = 'pages/tabbar/music/index?type=1'
									}else if(item.remindType == 2){
										item.pushTitle = '冥想提醒'
										item.pushContent = '今日冥想时间到啦，请进入无忧开始冥想吧。'
										item.pushPayload = 'pages/tabbar/music/index?type=2'
									}else{
										item.pushTitle = '呼吸提醒'
										item.pushContent = '今日呼吸时间到啦，请进入无忧开始呼吸吧。'
										item.pushPayload = 'pages/comA/breathe/breathe'
									}
									item.isOpen = true
									that.timePush(item)
								}else{
									item.isOpen = false
								}
							})
							that.list = res.data
							console.log(that.list)
						}
					},
					fail(err) {
							
					},
				});
				
				
			},
			getDays(){
				let day = uni.$_momentTime(moment(),'YYYY-MM-DD HH:mm:ss')
				let weekList = uni.$_getWeekDataList(day)
				this.dateList = []
				let dateList = []
				let weeks = {
				  "6":'周日',
				  "0":'周一',
				  "1":'周二',
				  "2":'周三',
				  "3":'周四',
				  "4":'周五',
				  "5":'周六',
				}
				let today = uni.$_momentTime(moment(),'YYYY-MM-DD')
				weekList.map((item,index) =>{
					let dateObj = {
						week: weeks[index],
						day: item.slice(8,10),
					    date:uni.$_momentTime(item,"YYYY-MM-DD"),
						isActive:item==today?true:false
					}
					dateList.push(dateObj);
				})
				this.dateList = dateList
				this.getList()
				// console.log(this.dateList,666)
				// return dateObj
			
			},
			timePush(data){
				// 假设现在是上午10点，我们想要在下午3点30分执行代码
				console.log("push内容：",data)
				let arr = []
				arr = data.timeList
				let that =this
				var now = new Date();
				arr.map(item =>{
					let target = new Date(item)
					var delay = target - now;
					if (delay > 0) { // 确保延迟时间是正数
					    setTimeout(function() {
					        // console.log('指定时间点到达，执行代码');
							that.pushNewsFn(data.pushTitle,data.pushContent,data.pushPayload)
					    }, delay);
					} else {
					    // console.log('现在已经过了指定的时间点，请在下一个时间点再试。');
					}
				})
				
			},
			switchChange(e,index){
				if(e){
					this.list[index].status = 1 
				}else{
					this.list[index].status = 0
				}
				this.doEdit(this.list[index])
			},
			doEdit(e){
				let that = this
				let p = {
					id:e.id,
					remindTime:e.remindTime,
					remindType:e.remindType,
					repeatTitle:e.repeatTitle,
					repeatType:e.repeatType,
					remindTimeList:e.remindTimeList,
					status:e.status
				}
				uni.$_post({
					url: "appUserRemind-add",
					that: that,
					params: p,
					needlogin: false,
					success(res) {
						if(res.code == 0){
							that.getDays()
						}
					},
					fail(err) {
							
					},
				});
			},
			delClick(e){
				let p = [e]
				let that = this
				uni.$_post({
					url: "appUserRemind-del",
					that: that,
					params: p,
					needlogin: false,
					success(res) {
						if(res.code == 0){
							that.$u.toast('删除成功！');
							that.list = that.list.filter(item=> item.id != e)
							that.getDays()
							// if(that.list.length==0){
							// 	that.getList()
							// }
						}
					},
					fail(err) {
							
					},
				});
				
				
			},
			pushNewsFn(title,content,payload) {   //你的处理逻辑的函数
				let clientId = uni.getStorageSync('unipush_id')
				let that = this
				let data = {
					title:title,
					content:content,
					cid:clientId,
					payload:payload,
					path:payload
				}
				uni.$_post({
					url: 'https://fc-mp-d0f8413b-fe52-4bf4-a9d4-ea64a93a939d.next.bspapp.com/wuyouPush',
					that: that,
					params: data,
					success(res) {
						// console.log('发送通知',res)
					},
					fail(err) {
						// uni.showToast({
						// 	title: err.msg,
						// 	duration: 1000,
						// 	icon:'error'
						// });	
						// console.log('错误信息',err)
					},
				});
			 
			},
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if(index != idx) this.list[idx].show = false;
				})
			},
			titleBack(){
				uni.navigateTo({
					url:'/pages/tabbar/mine/warn-edit'
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
.content{
	padding: 30rpx 30rpx;
	.list{
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(0,0,0,0.10); 
		background: #ffffff;
		border-radius: 24rpx;
		overflow: hidden;
		margin-bottom: 30rpx;
	}
	.item{
		// height: 180rpx;
		overflow: hidden;
		padding: 30rpx 40rpx;
		width: 100%;
		box-sizing: border-box;
		position: relative;
		.text{
			overflow: hidden;
			.title{
				font-size: 30rpx;
				font-family: $uni-font-family-Bold;
				color: #171717;
				margin-bottom: 10rpx;
			}
			.time{
				font-size: 46rpx;
				font-family: $uni-font-family-Heavy;
				color: #ccc;
				span{
					color: #848484;
					font-size: 24rpx;
					margin-left: 15rpx;
				}
			}
		}
	}
}
</style>
